//
//  HWSubscribeDesCell.m
//  HuaWan
//
//  Created by HuaWan  on 2022.
//  Copyright © 2022 HuaWan . All rights reserved.
//

#import "HWSubscribeDesCell.h"

@implementation HWSubscribeDesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setData:(id)data delegate:(id)delegate {
    if ([data isKindOfClass:[NSDictionary class]]) {
        if ([data[@"data"] isKindOfClass:[HWSubscribeDesCellModel class]]) {
            HWSubscribeDesCellModel *model = data[@"data"];
            
            if (model.time1.length > 0 && model.time2.length > 0 && model.time3.length > 0) {
                _titleLabel.attributedText = [self getNewColorStr:model.time1 headFont:[UIFont systemFontOfSize:15] headColor:UIColorFromHex(0xFFFFFF) middleStr:[NSString stringWithFormat:@"%@",model.time2] middleFont:[UIFont boldSystemFontOfSize:19] middleColor:UIColorFromHex(0xFFFFFF) footStr:model.time3 footFont:[UIFont systemFontOfSize:15] footColor:UIColorFromHex(0xFFFFFF)];
            } else {
                NSInteger font = 12;
                if (model.font > 0) {
                    font = model.font;
                }
                
                UIColor *color = UIColorFromHex(0xFFFFFF);
                _myTextView.hidden = YES;
                _titleLabel.hidden = NO;
                if (font == 24) {
                    color = UIColorFromHex(0xFFFFFF);
                    _titleLabel.textAlignment = NSTextAlignmentCenter;
                } else if (font == 17) {
                    color = UIColorFromHex(0xFFFFFF);
                    _titleLabel.textAlignment = NSTextAlignmentCenter;
                } else if (font == 10) {
                    color = UIColorFromHexA(0xFFFFFF,0.5);
                    _titleLabel.textAlignment = NSTextAlignmentLeft;
                    model.time2 = @" ";
                    _myTextView.text = model.time1;
                    _myTextView.hidden = NO;
                    _titleLabel.hidden = YES;
                    return;
                }
                _titleLabel.attributedText = [self getNewColorStr:model.time1 headFont:font == 24 ? [UIFont boldSystemFontOfSize:font] : [UIFont systemFontOfSize:font] headColor:color middleStr:[NSString stringWithFormat:@"%@",model.time2] middleFont:[UIFont boldSystemFontOfSize:font] middleColor:color footStr:model.time3 footFont:[UIFont systemFontOfSize:font] footColor:color];
            }
        }
    }
}

- (NSMutableAttributedString *)getNewColorStr:(NSString *)headStr headFont:(UIFont *)headFont headColor:(UIColor *)headColor middleStr:(NSString *)middleStr middleFont:(UIFont *)middleFont middleColor:(UIColor *)middleColor footStr:(NSString *)footStr footFont:(UIFont *)footFont footColor:(UIColor *)footColor {
    NSString *s = [NSString stringWithFormat:@"%@%@%@",headStr,middleStr,footStr];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:s];
    [str addAttribute:NSForegroundColorAttributeName value:headColor range:NSMakeRange(0,headStr.length)];
    [str addAttribute:NSForegroundColorAttributeName value:middleColor range:NSMakeRange(headStr.length, middleStr.length)];
    [str addAttribute:NSForegroundColorAttributeName value:footColor range:NSMakeRange(headStr.length+middleStr.length, footStr.length)];
    
    [str addAttribute:NSFontAttributeName value:headFont range:NSMakeRange(0,headStr.length)];
    [str addAttribute:NSFontAttributeName value:middleFont range:NSMakeRange(headStr.length, middleStr.length)];
    [str addAttribute:NSFontAttributeName value:footFont range:NSMakeRange(headStr.length+middleStr.length, footStr.length)];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
    if ([middleStr isEqualToString:@" "]) {
        paragraphStyle.alignment = NSTextAlignmentLeft;
    } else {
        paragraphStyle.alignment = NSTextAlignmentCenter;
    }
    [str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [s length])];
    
    return str;
}

+ (CGRect)boundsWithFontSize:(CGFloat)fontSize text:(NSString *)text needWidth:(CGFloat)needWidth lineSpacing:(CGFloat )lineSpacing {
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = lineSpacing;
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    [attributeString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
    [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attributeString boundingRectWithSize:CGSizeMake(needWidth, CGFLOAT_MAX) options:options context:nil];
    return rect;
}

+ (float)getCellFrame:(id)msg {
    
    if ([msg isKindOfClass:[HWSubscribeDesCellModel class]]) {
        HWSubscribeDesCellModel *model = msg;
        
        NSString *str = [NSString stringWithFormat:@"%@%@%@",model.time1,model.time2,model.time3];
        
        CGRect rect;
        if (model.time1.length > 0 && model.time2.length > 0 && model.time3.length > 0) {
            rect = [self boundsWithFontSize:15 text:str needWidth:kScreenWidth - 40*2 lineSpacing:5];
        } else {
            NSInteger font = 12;
            if (model.font > 0) {
                font = model.font;
            }
            
            if (font == 10) {
                return 60;
            }
            
            rect = [self boundsWithFontSize:font text:str needWidth:kScreenWidth - 40*2 lineSpacing:5];
        }
        return rect.size.height + 20;
    }
    
    return 60;
}

@end


#pragma mark - Model

@implementation HWSubscribeDesCellModel

+ (HWSubscribeDesCellModel *)ittemModelWithTime:(NSString *)time time2:(NSString *)time2 time3:(NSString *)time3 font:(NSInteger)font {
    HWSubscribeDesCellModel *model = [HWSubscribeDesCellModel new];
    model.time1 = kSafeString(time);
    model.time2 = kSafeString(time2);
    model.time3 = kSafeString(time3);
    model.font = font;
    return model;
}

@end
